package com.endes.android.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC03;
import com.endes.android.model.database.EndesOpenHelper;

public class QSSec3Dao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSSec3Dao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC03 ObtenerSeccion3(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada) {
		SSEC03 ssec03 = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC03 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			ssec03 = new SSEC03();
			ssec03.setQsCluster(qsCluster);
			ssec03.setQsNumber(qsNumber);
			ssec03.setQsHome(qsHome);
			ssec03.setQs20C(numeroOrdenPersonaSeleccionada);
			ssec03.setQs301(cursor.getInt(cursor.getColumnIndex("qs301")));
			ssec03.setQs302U(cursor.getInt(cursor.getColumnIndex("qs302U")));
			ssec03.setQs302C(cursor.getInt(cursor.getColumnIndex("qs302C")));
			ssec03.setQs303(cursor.getInt(cursor.getColumnIndex("qs303")));
			ssec03.setQs303XC(cursor.getString(cursor.getColumnIndex("qs303XC")));
			ssec03.setQs303X(cursor.getString(cursor.getColumnIndex("qs303X")));
			ssec03.setQs304(cursor.getInt(cursor.getColumnIndex("qs304")));
			ssec03.setQs305U(cursor.getInt(cursor.getColumnIndex("qs305U")));
			ssec03.setQs305C(cursor.getInt(cursor.getColumnIndex("qs305C")));
			ssec03.setQs306(cursor.getInt(cursor.getColumnIndex("qs306")));
			ssec03.setQs307(cursor.getInt(cursor.getColumnIndex("qs307")));
			ssec03.setQs308(cursor.getInt(cursor.getColumnIndex("qs308")));
			ssec03.setQs309(cursor.getInt(cursor.getColumnIndex("qs309")));
			ssec03.setQs311(cursor.getInt(cursor.getColumnIndex("qs311")));
			ssec03.setQs312U(cursor.getInt(cursor.getColumnIndex("qs312U")));
			ssec03.setQs312C(cursor.getInt(cursor.getColumnIndex("qs312C")));
			ssec03.setQs313(cursor.getInt(cursor.getColumnIndex("qs313")));
			ssec03.setQs313XC(cursor.getString(cursor.getColumnIndex("qs313XC")));
			ssec03.setQs313X(cursor.getString(cursor.getColumnIndex("qs313X")));
		}
		Cerrar();
		return ssec03;
	}

	public void guardarSec3Encuesta(SSEC03 ssec03) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs301", ssec03.getQs301());
		contenido.put("qs302U", ssec03.getQs302U());
		contenido.put("qs302C", ssec03.getQs302C());
		contenido.put("qs303", ssec03.getQs303());
		contenido.put("qs303XC", ssec03.getQs303XC());
		contenido.put("qs303X", ssec03.getQs303X());
		contenido.put("qs304", ssec03.getQs304());
		contenido.put("qs305U", ssec03.getQs305U());
		contenido.put("qs305C", ssec03.getQs305C());
		contenido.put("qs306", ssec03.getQs306());
		contenido.put("qs307", ssec03.getQs307());
		contenido.put("qs308", ssec03.getQs308());
		contenido.put("qs309", ssec03.getQs309());
		if(verificarEdadPregunta23(ssec03.getQsCluster(), ssec03.getQsNumber(),
				ssec03.getQsHome(), ssec03.getQs20C()) > 60){
			contenido.put("qs311", ssec03.getQs311());
			contenido.put("qs312U", ssec03.getQs312U());
			contenido.put("qs312C", ssec03.getQs312C());
			contenido.put("qs313", ssec03.getQs313());
			contenido.put("qs313XC", ssec03.getQs313XC());
			contenido.put("qs313X", ssec03.getQs313X());
		}
		if (ExisteSSEC03(ssec03)) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSEC03",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
					new String[] { ssec03.getQsCluster() + "",
							ssec03.getQsNumber() + "", ssec03.getQsHome() + "",
							ssec03.getQs20C() + "" });
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssec03.getQsCluster());
			contenido.put("qsNumber", ssec03.getQsNumber());
			contenido.put("qsHome", ssec03.getQsHome());
			contenido.put("qs20C", ssec03.getQs20C());
			db.insert("SSEC03", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSEC03(SSEC03 ssec03) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db.rawQuery(
				"Select * from SSEC03 where qsCluster=? and qsNumber=? and qsHome=? "
						+ "and qs20C=?", new String[] {
						ssec03.getQsCluster() + "", ssec03.getQsNumber() + "",
						ssec03.getQsHome() + "", ssec03.getQs20C() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}
	
	public int verificarEdadPregunta23(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int edad = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qs23 from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			edad = cursor.getInt(cursor.getColumnIndex("qs23"));
		}
		return edad;
	}

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
